﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace QueryDemo
{
	/// <summary>
	/// Provides the properties pane for the web part.
	/// </summary>
	public sealed class QueryDemoEditorPart : EditorPart
	{
		private bool _error;

		private QueryDemoEditorControl _editorControl;

		protected override void CreateChildControls()
		{
			if (!_error)
			{
				try
				{
					// Load user control for display of this editor part
					base.CreateChildControls();
					_editorControl = (QueryDemoEditorControl)Page.LoadControl("~/_controltemplates/QueryDemo/QueryDemoEditorControl.ascx");
					Controls.Add(_editorControl);

					// Set data source for query types
					_editorControl.QueryTypeDataSource = Enum.GetNames(typeof(QueryType));

					// Populate list control of lists within current web
					PopulateListOfLists();

					// Sets an initial row limit
					_editorControl.RowLimit = 10;

					// Binds all data within the editor control
					_editorControl.DataBind();
				}
				catch (Exception ex)
				{
					HandleException(ex);
				}
			}
		}

		protected override void OnLoad(EventArgs e)
		{
			if (!_error)
			{
				try
				{
					base.OnLoad(e);
					EnsureChildControls();
				}
				catch (Exception ex)
				{
					HandleException(ex);
				}
			}
		}

		/// <summary>
		/// Clear all child controls and add an error message for display.
		/// </summary>
		/// <param name="ex"></param>
		private void HandleException(Exception ex)
		{
			_error = true;
			Controls.Clear();
			Controls.Add(new LiteralControl(ex.ToString()));
		}

		/// <summary>
		/// Saves the values in an <see cref="T:System.Web.UI.WebControls.WebParts.EditorPart"/> control to the corresponding properties in the associated <see cref="T:System.Web.UI.WebControls.WebParts.WebPart"/> control.
		/// </summary>
		/// <returns>
		/// true if the action of saving values from the <see cref="T:System.Web.UI.WebControls.WebParts.EditorPart"/> control to the <see cref="T:System.Web.UI.WebControls.WebParts.WebPart"/> control is successful; otherwise (if an error occurs), false. 
		/// </returns>
		public override bool ApplyChanges()
		{
			EnsureChildControls();
			var targetPart = (QueryDemoWebPart)WebPartToEdit;
			targetPart.TypeOfQuery = _editorControl.TypeOfQuery;
			targetPart.ListTitle = _editorControl.ListTitle;
			targetPart.RowLimit = _editorControl.RowLimit;
			return true;
		}

		/// <summary>
		/// Retrieves the property values from a <see cref="T:System.Web.UI.WebControls.WebParts.WebPart"/> control for its associated <see cref="T:System.Web.UI.WebControls.WebParts.EditorPart"/> control.
		/// </summary>
		public override void SyncChanges()
		{
			EnsureChildControls();
			var sourcePart = (QueryDemoWebPart)WebPartToEdit;
			_editorControl.TypeOfQuery = sourcePart.TypeOfQuery;
			_editorControl.ListTitle = sourcePart.ListTitle;
			_editorControl.RowLimit = sourcePart.RowLimit;
		}

		/// <summary>
		/// Loops through lists in the current web and populates a collection of ListMetadata objects.
		/// </summary>
		private void PopulateListOfLists()
		{
			var listMetadata = new Collection<ListMetadata>();
			foreach (SPList list in SPContext.Current.Web.Lists)
			{
				listMetadata.Add(new ListMetadata(list));
			}

			_editorControl.ListDataSource = listMetadata;
		}
	}
}
